package com.example;


import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class TestIO {
    private static final String FILE_NAME = "log\\test.txt";

    private static final String FILE_PATH = "D:\\develop\\gitCode\\java_base\\javaBaisc_introduction\\javaBasic_IO\\src\\main\\resources\\";

    @Test
    public void testFileMethod() throws IOException, ParseException {
        File file = new File(FILE_NAME);

        if (!file.exists()) {
            file.createNewFile();
        }

        System.out.println("文件是否存在：" + file.exists());      // 文件是否存在
        System.out.println("是否为文件：" + file.isFile());      // 是否为文件
        System.out.println("是否为目录：" + file.isDirectory()); // 是否为目录
        System.out.println("是否可读：" + file.canRead());     // 是否可读
        System.out.println("是否可写：" + file.canWrite());    // 是否可写
        System.out.println("文件大小：" + file.length());      // 文件大小
        System.out.println("文件最后修改时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()))); // 文件最后修改时间
        System.out.println("文件名：" + file.getName());     // 文件名
        System.out.println("文件路径：" + file.getPath());     // 文件路径
        System.out.println("绝对路径：" + file.getAbsolutePath()); // 绝对路径
        System.out.println("父目录：" + file.getParent());   // 父目录
        System.out.println("父目录是否存在：" + file.renameTo(new File(FILE_PATH + "test2.txt"))); // 重命名
        System.out.println("文件是否存在：" + Arrays.toString(file.list()));        // 列出目录下的文件
        System.out.println("文件是否存在：" + Arrays.toString(file.listFiles()));   // 列出目录下的文件，并返回File数组
        System.out.println("是否可执行：" + file.canExecute());                  // 是否可执行
    }

    public static void logFileWrite(String message, String location) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fileName = sdf.format(new Date()) + ".log";
        String time = sdf2.format(new Date());

        File file = new File(FILE_PATH + "\\" + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }

        try (
                FileWriter fw = new FileWriter(file, true);
                BufferedWriter bw = new BufferedWriter(fw)
        ) {
            String content = "时间是：" + time + ",信息是：" + message + ", 位置是：" + location + "\n";
            //String content = location + "\n";
            bw.write(content);
            bw.flush();
        }
    }

    public static void logFileWrite(String message, String location, String fileName) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time = sdf2.format(new Date());

        File file = new File(FILE_PATH + "\\" + fileName + ".log");
        if (!file.exists()) {
            file.createNewFile();
        }

        try (
                FileWriter fw = new FileWriter(file, true);
                BufferedWriter bw = new BufferedWriter(fw)
        ) {
            String content = "时间是：" + time + ",信息是：" + message + ", 位置是：" + location + "\n";
            //String content = location + "\n";
            bw.write(content);
            bw.flush();
        }
    }


    @Test
    public void bijiao() throws IOException {
        List<String> original = OriginalData.getOriginalData();
        List<String> newList = OriginalData.getBiJiaoData();
        for (int i = 0; i < original.size(); i++) {
            if ("#N/A".equals(newList.get(i))) {
                logFileWrite(original.get(i),"#N/A");
            }
        }
    }
}